package com.design.composite.web.action;

import com.design.composite.bean.ResponseResult;
import com.design.composite.service.IRecordsService;
import com.design.composite.web.param.GenerateInputParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

/**
 * @author liuyajun
 * @desc 结果物控制层
 */
@RestController
@RequestMapping(value = "/record")
public class RecordsController {
    @Autowired
    private IRecordsService recordsService;

    /**
     * 预览效果图
     *
     * @return
     */
    @RequestMapping("/preView")
    public ResponseResult preView(@RequestBody List<GenerateInputParam> paramList) {
        try {
            return recordsService.preView(paramList);
        } catch (Exception e) {
            return ResponseResult.fail("预览效果图异常:" + e.getMessage());
        }
    }


    /**
     * 随机组合
     *
     * @return
     */
    @RequestMapping("/random")
    public ResponseResult random(@RequestBody List<GenerateInputParam> paramList) {
        try {
            return recordsService.random(paramList);
        } catch (Exception e) {
            return ResponseResult.fail("随机组合异常:" + e.getMessage());
        }
    }

    /**
     * 获取随机组合结果列表
     *
     * @return
     */
    @RequestMapping("/randomResults")
    public ResponseResult randomResults() {
        try {
            return recordsService.randomResults();
        } catch (Exception e) {
            return ResponseResult.fail("随机组合结果获取异常:" + e.getMessage());
        }
    }

    /**
     * 组合结果批量保存到指定路径
     *
     * @return
     */
    @RequestMapping("/randomTransform")
    public ResponseResult randomTransform(String toPath) {
        try {
            String last = toPath.substring(toPath.length()-1);
            if (!last.equals("\\") && !last.equals("/")){
                toPath = toPath + "\\";
            }
            File toFile = new File(toPath);
            if (!toFile.exists() || !toFile.isDirectory()) {
                return ResponseResult.fail("该目录不存在，请重新填写路径。");
            }
            recordsService.randomTransform(toPath);
            return ResponseResult.success(null);
        } catch (Exception e) {
            return ResponseResult.fail("组合结果批量异常:" + e.getMessage());
        }
    }


}
